package com.chenjiajin.service.impl;

import com.chenjiajin.domain.MybatisDynamic;
import com.chenjiajin.dto.QueryDTO;
import com.chenjiajin.dto.QueryUpdateBatchDTO;
import com.chenjiajin.mapper.MybatisDynamicMapper;
import com.chenjiajin.service.IMybatisDynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MybatisDynamicServiceImpl implements IMybatisDynamicService {

    @Autowired
    private MybatisDynamicMapper mapper;


    @Override
    public List<MybatisDynamic> query(QueryDTO qo) {
        System.out.println(qo);
        List<MybatisDynamic> query = mapper.query(qo);

        query.forEach(System.err::println);

        return query;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDynamic(QueryUpdateBatchDTO qo) {
        // update mybatis_dynamic_demo SET `name` = ?, `password` = ?, message = ?, age = ?, create_time = ?, update_time = ? where id = ? ;
        // update mybatis_dynamic_demo SET `name` = ?, `password` = ?, message = ?, age = ?, create_time = ?, update_time = ? where id = ?
        // 需要配置文件开启allowMultiQueries功能
        // jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true
        mapper.updateDynamic(qo);

        // 通过case，when 实现批量更新 , 可以动态修改集合中的每一个对象中的随意属性, 不是统一修改指定列
        mapper.updateDynamicBatch(qo);





    }
}

